# This is the base utility.
module Cobra; module Util

  ##
  # Locale - it holds the locale info.
  # e.g instace Locale.
  # Locale.new("en_US"), Locale.new("zh_CN")
  #
  class Locale
    DEFAULT_LOCALE_STRING = "en_US"
    
    attr_accessor :lang, :country
    
    def initialize(localeStr)
      @lang, @country = localeStr.split(/_/)
    end
  
    def getLang ()
      return @lang
    end
    
    def getCountry ()
      return @country
    end
      
    def to_s
      return  @lang + "_" + @country
    end
  end
    
  ##
  # Context - the container class 
  #
  class Context  
    attr_accessor :locale, :user
    private :locale, :user
      
    def initialize()
      @locale = Cobra::Util::Locale.new(Cobra::Util::Locale::DEFAULT_LOCALE_STRING)
    end
    
    def set_locale (locale)
      @locale = locale
    end
    
    def get_locale ()
      return @locale
    end
      
    def get_user
      return @user
    end

    def set_user (user)
      @user = user
    end
              
    def to_s
      return self.class.to_s + ",locale:" + @locale.to_s + ",user:" + @user.to_s + "\n"
    end
  end
    
    

  # Security error. Controllers throw these in situations where a user is trying to access a
  # function that he is not authorized to access. 
  # Normally, RForum does not show URLs that would allow the user to access such features.
  class SystemError < StandardError
    attr_accessor :errorMsg
    
    def initialize(name = nil)        
      @name = name
    end
    
    def set_local_msg(errorMsg)
      @errorMsg = errorMsg
    end
    
    def get_local_msg(context)
      return @errorMsg if @errorMsg
      return _(context, @name)
    end
    
    def to_s
      return @name
    end
    
  end

end;end #end of module Cobra::Util


##
# it herits Object class.
#
class Object
  # localizing method #
  def _(*args)
    localeInfo = args[0]
    msgKey = args[1]
    
    msgVariables = nil
    if args.size >2
      msgVariables = args[2]
    end
    
    return nil if msgKey == nil
    
    # Convert Symbol to String
    msgKey = msgKey.to_s
    return nil if msgKey.empty?
    
    if localeInfo.is_a?(Cobra::Util::Locale) then
      return Cobra::I18N.l(localeInfo.to_s, msgKey, msgVariables)       
    else localeInfo.is_a?(Cobra::Util::Context)
      locale = localeInfo.get_locale()  
      Cobra::I18N.l(locale.to_s, msgKey, msgVariables)
    end            
  end
end

class Logger
  def format_message(severity, timestamp, progname, msg)        
    #str = "#{timestamp.strftime("%Y-%m-%d %H:%M:%S")} ["
    str = "#{timestamp} ["
    if progname
        str << progname 
        str << ":" 
    end
    str << "#{severity}] #{msg}\n"
  end
end

# String Extention - Useful methods.
class String
  # Start With
  # return true if start position match prefix, else return false
  def start_with(prefix)
    return  ((self =~ /^#{prefix}/) != nil)
  end
  
  # End With Suffix
  # return true if last position match suffix, else return false.
  def end_with(suffix)
    return (self =~ /#{suffix}$/) != nil
  end
end    







